package com.example.demo.controller;


import com.example.demo.utils.ExcelUtil;
import com.example.demo.utils.ExcelUtilX;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * @author 谢辉
 * @Classname XiehController
 * @Description TODO
 * @Date 2020/12/9 18:00
 */
@Controller
@RequestMapping(value = "/xieh")
public class XiehController {

    @RequestMapping(value = "/export")
    @ResponseBody
    public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<ExcelUtilX.TitileBean> title = new ArrayList<>();
        ExcelUtilX.TitileBean titileBean1 = new ExcelUtilX.TitileBean();
        ExcelUtilX.TitileBean titileBean2 = new ExcelUtilX.TitileBean();
        titileBean1.setId("name");
        titileBean1.setName("名字");
        titileBean2.setId("age");
        titileBean2.setName("年龄");
        title.add(titileBean1);
        title.add(titileBean2);

        List<Map<String, Object>> data = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("name", "谢辉");
        map.put("age", 23);
        data.add(map);

        //excel文件名
        String fileName = "学生信息表.xls";
        //sheet名
        String sheetName = "学生信息表";

        //创建HSSFWorkbook
        Workbook wb = ExcelUtilX.getHSSFWorkbook(sheetName, title, data, null);

        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/export2")
    @ResponseBody
    public void export2(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<LinkedHashMap<String, Object>> data = new ArrayList<>();
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        map.put("name", "谢辉");
        map.put("age", 23);
        data.add(map);

        //excel文件名
        String fileName = "学生信息表2.xlsx";
        //sheet名
        String sheetName = "学生信息表";

        //创建HSSFWorkbook
        Workbook wb = ExcelUtilX.getHSSFWorkbook(sheetName, data, new XSSFWorkbook());

        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/export3")
    @ResponseBody
    public void export3(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取数据
        // List<PageData> list = reportService.bookList(page);
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map1 = new LinkedHashMap<String, String>();
        map1.put("stuName", "谢辉");
        map1.put("stuSex", "男");
        map1.put("stuAge", "23");
        map1.put("stuSchoolName", "小学");
        map1.put("stuClassName", "2");
        list.add(map1);

        //excel标题
        String[] title = {"名称", "性别", "年龄", "学校", "班级"};
        // 内容
        String[][] content = new String[list.size()][title.length];
        //excel文件名
        String fileName = "学生信息表3.xlsx";
        //sheet名
        String sheetName = "学生信息表";
        for (int i = 0; i < list.size(); i++) {
            Map<String, String> obj = list.get(i);
            content[i][0] = obj.get("stuName");
            content[i][1] = obj.get("stuSex");
            content[i][2] = obj.get("stuAge");
            content[i][3] = obj.get("stuSchoolName");
            content[i][4] = obj.get("stuClassName");
        }

        //创建HSSFWorkbook
        //HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        Workbook wb = ExcelUtilX.getHSSFWorkbook(sheetName, title, content, new XSSFWorkbook());

        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //发送响应流方法
    private void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
